package com.sh.business.controller;

import java.util.List;

import com.sh.business.domain.vo.BizUserImportVo;
import com.sh.business.domain.vo.BizUserOutInvestImportVo;
import com.sh.business.listener.BizUserImportListener;
import com.sh.business.listener.BizUserOutInvestImportListener;
import com.sh.common.excel.core.ExcelResult;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.sh.common.idempotent.annotation.RepeatSubmit;
import com.sh.common.log.annotation.Log;
import com.sh.common.web.core.BaseController;
import com.sh.common.mybatis.core.page.PageQuery;
import com.sh.common.core.domain.R;
import com.sh.common.core.validate.AddGroup;
import com.sh.common.core.validate.EditGroup;
import com.sh.common.log.enums.BusinessType;
import com.sh.common.excel.utils.ExcelUtil;
import com.sh.business.domain.vo.UserOutInvestVo;
import com.sh.business.domain.bo.UserOutInvestBo;
import com.sh.business.service.IUserOutInvestService;
import com.sh.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户外部投资记录
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/userOutInvest")
public class UserOutInvestController extends BaseController {

    private final IUserOutInvestService userOutInvestService;

    /**
     * 查询用户外部投资记录列表
     */
    @SaCheckPermission("business:userOutInvest:list")
    @GetMapping("/list")
    public TableDataInfo<UserOutInvestVo> list(UserOutInvestBo bo, PageQuery pageQuery) {
        return userOutInvestService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户外部投资记录列表
     */
    @SaCheckPermission("business:userOutInvest:export")
    @Log(title = "用户外部投资记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserOutInvestBo bo, HttpServletResponse response) {
        List<UserOutInvestVo> list = userOutInvestService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户外部投资记录", UserOutInvestVo.class, response);
    }

    /**
     * 获取用户外部投资记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:userOutInvest:query")
    @GetMapping("/{id}")
    public R<UserOutInvestVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userOutInvestService.queryById(id));
    }

    /**
     * 新增用户外部投资记录
     */
    @SaCheckPermission("business:userOutInvest:add")
    @Log(title = "用户外部投资记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserOutInvestBo bo) {
        return toAjax(userOutInvestService.insertByBo(bo));
    }

    /**
     * 修改用户外部投资记录
     */
    @SaCheckPermission("business:userOutInvest:edit")
    @Log(title = "用户外部投资记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserOutInvestBo bo) {
        return toAjax(userOutInvestService.updateByBo(bo));
    }

    /**
     * 删除用户外部投资记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:userOutInvest:remove")
    @Log(title = "用户外部投资记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userOutInvestService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入投资
     *
     * @param file          导入文件
     */
    @Log(title = "导入投资", businessType = BusinessType.IMPORT)
    @SaCheckPermission("business:userOutInvest:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<BizUserOutInvestImportVo> result = ExcelUtil.importExcel(file.getInputStream(), BizUserOutInvestImportVo.class, new BizUserOutInvestImportListener());
        return R.ok(result.getAnalysis());
    }
}
